﻿package view
{
	import controller.GameController;
	
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.net.FileReference;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	import model.Product;
	
	public class ProductWrapper extends Sprite
	{
		private var product:Product;
		private var posX:uint;
		private var posY:uint;
		private var loadPic:Loader;
		private var toTrack_tw:Tween;
		private var trackScanX_tw:Tween;
		private var trackFallingX_tw:Tween;		
		private var trackScanY_tw:Tween;
		private var trackFallingY_tw:Tween;	
		private var trackDownX_tw:Tween;
		private var trackDownY_tw:Tween;
		private var timer:Timer = new Timer(600, 1);
		
		public function ProductWrapper(product:Product, posX:uint, posY:uint):void
		{
			this.product = product;
			
			this.x = posX;
			this.y = posY;	
			
			this.loadPic = new Loader;
			this.loadPic.contentLoaderInfo.addEventListener(Event.COMPLETE, doInitialScaling);
			this.loadPic.load(new URLRequest(product.getPic()));
			
			this.addChild(loadPic);
		}
		
		//look for longest site, calculate scaling and scale pic
		public function doInitialScaling(event:Event):void
		{		
			var scaling:Number = 0.0;
			
			if (this.width >= this.height) {
				scaling = Config.ProductWidthInShelf / this.width;
			} else {
				scaling = Config.ProductHeightInShelf / this.height;
			}
			this.scale(scaling);
			this.doInitialPositioning();
			
			//trace(" Breite: " + this.width + " Höhe: " + this.height + " In Shelf: " + Config.ProductWidthInShelf + "  " + Config.ProductHeightInShelf + " Scaling: " +  scaling);						
		}
		
		private function doInitialPositioning():void 
		{
			if (this.width >= this.height) {
				//height not completly , put product to bottom
				this.y = this.y + (Config.ProductHeightInShelf - this.height);
				this.posY = this.y;
			} else { 	
				//width not completly filled, center product
				this.x = this.x + (Config.ProductWidthInShelf - this.width) / 2;
				this.posX = this.x;
			}		
		}
		
		public function scale(scaling:Number):void
		{
			this.scaleX = scaling;
			this.scaleY = scaling;
		}
		
		public function getX():uint{
			return posX;
		}
		
		public function getY():uint{
			return posY;
		}
		
		
		/**
		 * Bewegung der Produkte
		 */ 
		public function animateTo(productWrapper:ProductWrapper, track:Array, gameController:GameController):void {
			//ToDo  alle Tweens --> minimieren
			toTrack_tw = new Tween(productWrapper, "x", null, productWrapper.x, track[0][0], Config.productSpeed, true);
			toTrack_tw = new Tween(productWrapper, "y", null, productWrapper.y, track[0][1], Config.productSpeed, true);
			
			trackScanX_tw = new Tween (productWrapper, "x", null, track[0][0], track[2][0], Config.productSpeed, true);
			trackScanX_tw.stop();
			trackScanY_tw = new Tween (productWrapper, "y", null, track[0][1], track[2][1], Config.productSpeed, true);
			trackScanY_tw.stop();
			trackDownX_tw = new Tween (productWrapper, "x", null, track[2][0], track[1][0], Config.productSpeed, true);
			trackDownX_tw.stop();
			trackDownY_tw = new Tween (productWrapper, "y", null,  track[2][1], track[1][1], Config.productSpeed, true);
			trackDownY_tw.stop();
			trackFallingX_tw = new Tween (productWrapper, "x", null, track[1][0], track[3][0], Config.productSpeed, true);
			trackFallingX_tw.stop();
			trackFallingY_tw = new Tween (productWrapper, "y", null, track[1][1], track[3][1], Config.productSpeed, true);
			trackFallingY_tw.stop();
						
			toTrack_tw.addEventListener(TweenEvent.MOTION_FINISH, productReady);
			trackScanX_tw.addEventListener(TweenEvent.MOTION_FINISH, startTimer);
			trackDownX_tw.addEventListener(TweenEvent.MOTION_FINISH, fallDown);
			
			gameController.testHitting(productWrapper);
		}
		
		private function productReady(e:TweenEvent):void{
			trackScanX_tw.start();
			trackScanY_tw.start();
		} 
		
		private function startTimer(e:TweenEvent):void{
			this.product.getNameSound().play();
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, productRun);
			timer.start();
		}
		private function productRun(e:TimerEvent):void{
			trackDownX_tw.start();
			trackDownY_tw.start();
		}
		
		private function fallDown(e:TweenEvent):void{
			trackFallingX_tw.start();
			trackFallingY_tw.start();
		}
	}
}